﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EvTest.Zip
{
    public partial class ZipDir : Form
    {
        public ZipDir()
        {
            InitializeComponent();
        }

        private nsoftware.IPWorksZip.Zip zip1;
        private nsoftware.IPWorksZip.Tar tar1;
        private nsoftware.IPWorksZip.Jar jar1;

        private void bt1_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog FBD = new FolderBrowserDialog();
            FBD.SelectedPath = System.IO.Directory.GetCurrentDirectory();
            FBD.ShowDialog();
            tb1.Text = FBD.SelectedPath;
        }


        private void CompressUsingZip()
        {
            zip1 = new nsoftware.IPWorksZip.Zip();
            zip1.OnProgress += new nsoftware.IPWorksZip.Zip.OnProgressHandler(this.zip1_OnProgress);

            zip1.ArchiveFile = textBox1.Text.Trim() + tb4.Text;
            zip1.RecurseSubdirectories = cb1.Checked;
            zip1.Files.Clear();
            zip1.IncludeFiles(tb1.Text + tb2.Text);
            zip1.ExcludedFiles = tb3.Text;
            if (tbPassword.Text != "")
            {
                zip1.Password = tbPassword.Text.Trim();

            }

            zip1.Compress();
        }

        private void CompressUsingTar(bool UseGzipCompression)
        {
            tar1.ArchiveFile = tb4.Text;
            tar1.RecurseSubdirectories = cb1.Checked;
            tar1.Files.Clear();
            tar1.IncludeFiles(tb1.Text + tb2.Text);
            tar1.ExcludedFiles = tb3.Text;
            tar1.UseGzipCompression = UseGzipCompression;
            tar1.Compress();
        }

        private void CompressUsingJar()
        {
            jar1.ArchiveFile = tb4.Text;
            jar1.RecurseSubdirectories = cb1.Checked;
            jar1.Files.Clear();
            jar1.IncludeFiles(tb1.Text + tb2.Text);
            jar1.ExcludedFiles = tb3.Text;
            jar1.Compress();
        }

        private void bt2_Click(object sender, EventArgs e)
        {

            if (!tb1.Text.Equals("") && !tb1.Text.EndsWith("\\"))
                tb1.Text += '\\';
            progressBar1.Value = 0;

            try
            {
                if (rbZip.Checked) CompressUsingZip();
                else if (rbTar.Checked) CompressUsingTar(false);
                else if (rbJar.Checked) CompressUsingJar();
                else if (rbTar.Checked) CompressUsingTar(true);
                MessageBox.Show("Finished!");
            }
            catch (nsoftware.IPWorksZip.IPWorksZipException ipwe)
            {
                MessageBox.Show(ipwe.Message);
            }
        }


        private void zip1_OnProgress(object sender, nsoftware.IPWorksZip.ZipProgressEventArgs e)
        {
            progressBar1.Value = e.PercentProcessed;
        }

        private void tar1_OnProgress(object sender, nsoftware.IPWorksZip.TarProgressEventArgs e)
        {
            progressBar1.Value = e.PercentProcessed;
        }

        private void jar1_OnProgress(object sender, nsoftware.IPWorksZip.JarProgressEventArgs e)
        {
            progressBar1.Value = e.PercentProcessed;
        }

        private void ZipDir_Load(object sender, EventArgs e)
        {
            tb1.Text = System.IO.Directory.GetCurrentDirectory();
        }

        private void rbZip_CheckedChanged(object sender, EventArgs e)
        {
        }

        private void rbJar_Click(object sender, EventArgs e)
        {
            rbZip.Checked = false;
            //.......

            tb4.Text = "archive.jar";
        }
    }
}
